from distutils.command import upload
from tortoise import Tortoise, fields, models
import re
import os

DATABASE_URL = "sqlite://database.db"


class User(models.Model):
    id = fields.IntField(pk=True)
    user_telegram_id = fields.CharField(max_length=255, null=True)
    name = fields.CharField(max_length=255, null=True, unique=True)
    surname = fields.CharField(max_length=255, null=True)
    phone = fields.CharField(max_length=255, null=True)
    email = fields.CharField(max_length=255, null=True)
    password = fields.CharField(max_length=255, null=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "users"

    def __str__(self):
        return self.name


async def init():
    await Tortoise.init(
        db_url="sqlite://db.sqlite3",
        modules={"models": ["model.models"]},
    )
    await Tortoise.generate_schemas()


async def create_user(**kwargs):
    await User.create(**kwargs)


async def get_user_db(name: str):
    return await User.get_or_none(name=name)


async def get_users():
    users = await User.all()
    return users



